AUI().use('aui-io-request', 'aui-loading-mask', 'aui-node', 'event', 'node-event-simulate', 'aui-tooltip', 'event-base', 'aui-panel', 'aui-tabs', 'json', 'aui-dialog', 'aui-tree-view',
		'aui-overlay-manager','aui-overlay-mask', 'dd-constrain', 'yui', function(A){
	
	A.one("#"+portletNamespace+"otherCity").hide();
	
	getRegions= function() {
					
		var url = resourceUrl;
		var countryId = A.one("#"+portletNamespace+"countryId").val();

		AUI().io.request(url, {
			method: "GET",
			data: {
				"cmd":"get-regions",
				"countryId":countryId
			},
			dataType: 'json',
			on: {
				success: function() {
				populateRegionDropdown(this.get('responseData'));
				},
				failure: function(xhr, ajaxOptions, thrownError){
					alert("Error getting the list of Regions");
				}
			}
		});
	};
	
	populateRegionDropdown=function(data) {
    	
        var regions;

        regions = "<option value=''> select one </option>";
       
        for(var i=0; i< data.length; i++){
        	
        	regions += "<option value='"+ data[i].id +"'>" + data[i].name + "</option>";
            
        }

        if(A.UA.gecko > 0){
        	
            A.one("#"+portletNamespace+"regionId").html(regions);
            
        }
        else {
        	 A.one("#"+portletNamespace+"regionId").empty().append(regions);
        }
         A.one("#"+portletNamespace+"otherCity").hide();
    };
    
    getCities= function() {
    	
		var url = resourceUrl;
		var regionId = A.one("#"+portletNamespace+"regionId").val();

		AUI().io.request(url, {
			method: "GET",
			data: {
				"cmd":"get-cities",
				"regionId":regionId
			},
			dataType: 'json',
			on: {
				success: function() {
					populateCityDropdown(this.get('responseData'));
				},
				failure: function(xhr, ajaxOptions, thrownError){
					alert("Error getting the list of cities");
				}
			}
		});
	};
	
	populateCityDropdown=function(data) {
    	
        var cities;

        cities = "<option value=''> select one </option><option value='0' >Other city</option>";

        for(var i=0; i< data.length; i++){
        	
        	cities += "<option value='"+ data[i].id +"'>" + data[i].name + "</option>";
           }

        if(A.UA.gecko > 0){
        	
            A.one("#"+portletNamespace+"city").html(cities);
        } 
        
        else {
        	  A.one("#"+portletNamespace+"city").empty().append(cities);
            }
        A.one("#"+portletNamespace+"otherCity").hide();
    };
    
    enableOtherCity=function(){
    	
    	var cityId = A.one("#"+portletNamespace+"city").val();
    	
    	if(cityId =='0'){
    		
			 A.one("#"+portletNamespace+"otherCity").show();
		 }
    	
    	else{
    		 A.one("#"+portletNamespace+"otherCity").hide();
			}
    };
});